package com.tzxs.pay.util;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * 本地存储类
 */
public class ISharedPreference {
    private static ISharedPreference preference;

    private SharedPreferences sharedPreferences;//存储对象
    private boolean receiveNotice;

    private ISharedPreference(Application app) {

        sharedPreferences = app.getSharedPreferences("tz", Context.MODE_PRIVATE);
    }

    /**
     * 使用单例模式
     *
     * @param app app
     * @return
     */
    public static ISharedPreference getInstance(Application app) {
        if (preference == null) {
            synchronized (ISharedPreference.class) {
                if (preference == null) {
                    preference = new ISharedPreference(app);
                }
            }
        }
        return preference;
    }


    //退出登录   清除数据
    public void loginOut() {
        sharedPreferences.edit().clear().apply();
    }

    /**
     * 保存token
     *
     * @param token token
     */
    public void saveToken(String token) {
        sharedPreferences.edit().putString("token", token).apply();
    }

    public String getToken() {
        return sharedPreferences.getString("token", "");
    }

    /**
     * 保存用户id
     *
     * @param userId id
     */
    public void saveUserId(int userId) {
        sharedPreferences.edit().putInt("userId", userId).apply();
    }

    public int getUserId() {
        return sharedPreferences.getInt("userId", 0);
    }

    /**
     * 保存用户手机号
     *
     * @param phone 手机号
     */
    public void savePhone(String phone) {
        sharedPreferences.edit().putString("phone", phone).apply();
    }

    public String getPhone() {
        return sharedPreferences.getString("phone", "");
    }

    /**
     * 保存用户名
     *
     * @param userName 用户名
     */
    public void saveUserName(String userName) {
        sharedPreferences.edit().putString("userName", userName).apply();
    }

    public String getUserName() {
        return sharedPreferences.getString("userName", "");
    }

    //储存是否接收通知
    public void saveReceiveNotice(boolean is) {
        sharedPreferences.edit().putBoolean("is", is).apply();
    }

    //默认接收通知
    public boolean getReceiveNotice() {
        return sharedPreferences.getBoolean("is", true);
    }

    //储存是通知字符串
    public void saveNoticeStr(String notice) {
        sharedPreferences.edit().putString("notice", notice).apply();
    }

    public String getNoticeStr() {
        return sharedPreferences.getString("notice", "");
    }

    //保存cid
    public void saveCid(String cid) {
        sharedPreferences.edit().putString("cid", cid).apply();
    }

    //获取cid
    public String getCid() {
        return sharedPreferences.getString("cid", "");
    }
}
